package com.glut.campusrepair.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.glut.campusrepair.dto.MaintenanceLongRecordsdto;
import com.glut.campusrepair.dto.MaintenanceRecordsdto;
import com.glut.campusrepair.dto.MaintenanceShortRecordsdto;
import com.glut.campusrepair.entity.MaintenanceRecordRequest;
import com.glut.campusrepair.entity.MaintenanceRecords;
import com.glut.campusrepair.service.IMaintenanceRecordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  前端控制器
 *
 * @author QJC
 * @since 2024-07-21 17:24
 */
@RestController
@Slf4j
@CrossOrigin
public class MaintenanceRecordsController {

    @Autowired
    IMaintenanceRecordsService imaintenanceRecordsService;

    @PostMapping("/admin/page10/selectpages")
    public Page<MaintenanceRecords> queryMaintenanceRecords(@RequestBody MaintenanceRecordsdto maintenanceRecordsdto) {

        return imaintenanceRecordsService.querypages(maintenanceRecordsdto);
    }

    /**
     *  批量删除
     */
    @DeleteMapping("/admin/page10/deleteusers")
    public void deletes(@RequestBody List<Integer> ids) {
        log.info("ids数组: {}", ids);
        imaintenanceRecordsService.deletes(ids);
    }

    /**
     * 单个删除
     */
    @DeleteMapping("/admin/page10/deleteuser")
    public void delete(@RequestBody MaintenanceRecordsdto maintenanceRecordsdto){
        Integer id = maintenanceRecordsdto.getMainmaintenanceRecordsId();
        System.out.println("ID是: " + id); // 添加日志
        if (id == null) {
            throw new IllegalArgumentException("ID不可以是null");
        }
        imaintenanceRecordsService.deleteuser(maintenanceRecordsdto.getMainmaintenanceRecordsId());
    }

    @PutMapping("/admin/page10/updaterecord")
    public void updaterecord(@RequestBody MaintenanceRecordsdto maintenanceRecordsdto){
    /*    try {
            log.info("更新记录：{}", maintenanceRecordsdto);
            imaintenanceRecordsService.update(maintenanceRecordsdto);
        } catch (Exception e) {
            log.error("更新记录时发生错误：", e);
            throw e; // 再次抛出异常以便前端可以捕捉到
        }*/

        imaintenanceRecordsService.update(maintenanceRecordsdto);
    }


    /**韦思源
     * 根据当前登录用户ID查询这个用户报修的所有未评价的简洁版维修记录
     * 展示在维修记录卡片中
     * （已使用）
     */
    @PostMapping("/service-evaluation/getMaintenanceRecordsSimpleByUserNo_1/{userNo}")
    public List<MaintenanceShortRecordsdto> getMaintenanceRecordsSimpleByUserNo_1(@PathVariable Integer userNo) {
        log.info("userNo: {}", userNo);
        System.out.println("---------------"+userNo);
        return imaintenanceRecordsService.selectByUserNo_11(userNo);
    }

    /**韦思源
     * 根据当前登录用户ID查询这个用户报修的所有已评价的简洁版维修记录
     * 展示在维修记录卡片中
     * （已使用）
     */
    @PostMapping("/service-evaluation/getMaintenanceRecordsSimpleByUserNo_2/{userNo}")
    public List<MaintenanceShortRecordsdto> getMaintenanceRecordsSimpleByUserNo_2(@PathVariable Integer userNo) {
        return imaintenanceRecordsService.selectByUserNo_12(userNo);
    }


    /**韦思源
     * 根据维修记录ID查询详细版维修记录
     * 展示在详情抽屉中
     * （已使用）
     */
    @PostMapping("/service-evaluation/getMaintenanceRecordsDetailById/{maintenanceRecordsId}")
    public MaintenanceLongRecordsdto getMaintenanceRecordsById(@PathVariable Integer maintenanceRecordsId) {
        log.info("maintenanceRecordsId: {}", maintenanceRecordsId);
        return imaintenanceRecordsService.selectByPrimaryKey_2(maintenanceRecordsId);
    }

    @GetMapping("/maintenance-records/{employeeId}")
    public List<MaintenanceRecords> getEmployeeRepairRecords(@PathVariable Integer employeeId){
        return imaintenanceRecordsService.getEmployeeRepairRecords(employeeId);
    }

    @GetMapping("/maintenance-records/now/{employeeId}")
    public List<MaintenanceRecords> getNowEmployeeRepairRecords(@PathVariable Integer employeeId){
        return imaintenanceRecordsService.getNowEmployeeRepairRecords(employeeId);
    }

    @GetMapping("/maintenance-records/up/{employeeId}")
    public void upRepairTasks(@PathVariable Integer employeeId){
        imaintenanceRecordsService.upMainMaintenanceRecord(employeeId);
    }

    @PostMapping("/maintenance-records/upBackData")
    public MaintenanceRecords getMaintenanceRecords(@RequestBody MaintenanceRecordRequest maintenanceRecordRequest){
        return imaintenanceRecordsService.getMaintenanceRecords(maintenanceRecordRequest);
    }
}
